package com.bitdf.txing.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.chat.dao.AddFriendRequestDao;
import com.bitdf.txing.chat.entity.AddFriendRequestEntity;
import com.bitdf.txing.chat.entity.FriendRelationEntity;
import com.bitdf.txing.chat.service.AddFriendRequestService;
import com.bitdf.txing.chat.service.FriendRelationService;
import com.bitdf.txing.common.utils.MyException;
import com.bitdf.txing.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("addFriendRequestService")
public class AddFriendRequestServiceImpl extends ServiceImpl<AddFriendRequestDao, AddFriendRequestEntity> implements AddFriendRequestService {

    @Autowired
    FriendRelationService friendRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<AddFriendRequestEntity> page = this.page(
//                new Query<AddFriendRequestEntity>().getPage(params),
//                new QueryWrapper<AddFriendRequestEntity>()
//        );
//
//        return new PageUtils(page);
        return null;
    }

    @Override
    public AddFriendRequestEntity requestAddFriend(Long selfId, Long friendId) {

        List<Long> list=friendRelationService.findFriendIds(selfId);
        if (list.contains(friendId)){
            return null;
        }
        AddFriendRequestEntity entity = this.getOne(new QueryWrapper<AddFriendRequestEntity>().eq("send_user_id", selfId).eq("receive_user_id", friendId));
        if (entity!=null){
            return null;
        }

//        AddFriendRequestEntity addFriendRequestEntity = new AddFriendRequestEntity(null, selfId, friendId, 0, new Date(), new Date());
        AddFriendRequestEntity addFriendRequestEntity = new AddFriendRequestEntity();
        addFriendRequestEntity.setSendUserId(selfId);
        addFriendRequestEntity.setReceiveUserId(friendId);
        boolean save = this.save(addFriendRequestEntity);


        return save?addFriendRequestEntity:null;
    }

    @Override
    public List<AddFriendRequestEntity> getAddFriendRequest(Long selfId) {
        List<AddFriendRequestEntity> list = this.list(new QueryWrapper<AddFriendRequestEntity>().eq("send_user_id", selfId).or().eq("receive_user_id", selfId));
        return list;
    }

    @Override
    @Transactional
    public AddFriendRequestEntity agreeAddFriendRuquest(Long id, Long selfId) {
        //判断是否合法
        AddFriendRequestEntity entity = this.getById(id);
        if (!entity.getReceiveUserId().equals(selfId)){
            return null;
        }

        //修改请求状态
        entity.setStatus(1);
        entity.setUpdateTime(new Date());
        boolean b = this.updateById(entity);

        //添加好友
//        boolean save = friendRelationService.save(new FriendRelationEntity(null, selfId, entity.getSendUserId(), new Date()));
        FriendRelationEntity relationEntity = new FriendRelationEntity();
        relationEntity.setFriendId(entity.getSendUserId());
        relationEntity.setUserId(selfId);
        boolean save = friendRelationService.save(relationEntity);

        if(!b || !save){
            throw new MyException("数据库操作失败");
        }

        return entity;
    }

    @Override
    @Transactional
    public AddFriendRequestEntity disagreeAddFriendRuquest(Integer id, Long selfId) {
        //判断是否合法
        AddFriendRequestEntity entity = this.getById(id);
        if (!entity.getReceiveUserId().equals(selfId)){
            return null;
        }
        if (entity.getStatus()!=0){
            return null;
        }
        entity.setUpdateTime(new Date());
        entity.setStatus(2);
        boolean b = this.updateById(entity);
        return b?entity:null;
    }

}